/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import testcases.MainTest;

import com.trentech.billcalc.FinanceManager;


/**
 * TransferLeaseEvent allows the user to transfer a lease to another person 
 * without incurring the lease break penalty.
 * 
 * @author Trent Hoeppner
 */
public class TransferLeaseEvent extends Event {

    /**
     * The name of the tenant who will take over the lease.
     */
    private String newTenant;
    
    
    /**
     * True indicates that the new tenant will pay the deposit and remaining 
     * rent to the old tenant, false indicates that the landlord will pay the 
     * deposit and remaining rent to the old tenant, and collect it from the 
     * new tenant.
     */
    private boolean transferPaid;


    /**
     * Constructor for RenewLeaseEvent.
     * 
     * @param   date            the date that the lease was renewed (should be 
     *                          the end of the old lease).  Cannot be null.
     * @param   oldTenant       the name of the tenant who currently has the 
     *                          lease.  Cannot be null or empty.
     * @param   newTenant       the name of the tenant who will take over the 
     *                          lease. Cannot be null or empty.
     * @param   transferPaid    true indicates that the new tenant will give
     *                          the deposit and extra rent to the old tenant, 
     *                          false indicates that the landlord will give the
     *                          deposit and extra rent to the old tenant and 
     *                          collect them from the new tenant.
     */
    public TransferLeaseEvent(Date date, String oldTenant, String newTenant, 
            boolean transferPaid) {
        super(date, oldTenant);
        
        this.newTenant = newTenant;
        this.transferPaid = transferPaid;
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        TransferLeaseEvent otherCasted = (TransferLeaseEvent) other;
        value = newTenant.compareTo(otherCasted.getNewTenant());
        if (value != 0) {
            return value;
        }

        value = boolToValue(transferPaid) - boolToValue(otherCasted.isTransferPaid());
        if (value != 0) {
            return value;
        }
        
        return 0;
    }
    
    
    /**
     * {@inheritDoc}
     */
    public void process(FinanceManager manager) {
        manager.transferLease(getDate(), getName(), newTenant, transferPaid);
    }
    
    
    /**
     * Returns the name of the tenant who will take over the lease.
     * 
     * @return  the name of the new tenant.  Will not be null or empty.
     */
    public String getNewTenant() {
        return newTenant;
    }


    /**
     * Sets the name of the tenant who will take over the lease.
     * 
     * @param   newTenant   the name of the new tenant.  Cannot be null or 
     *                      empty.
     */
    public void setNewTenant(String newTenant) {
        this.newTenant = newTenant;
    }


    /**
     * Returns whether the deposit and remaining rent will be transferred 
     * between the old and new roommates.
     * 
     * @return  true if the roommates will transfer between each other, false 
     *          if they will transfer through the landlord.
     */
    public boolean isTransferPaid() {
    	// original : 
        // return transferPaid;
    	// seed fault : 
    	return transferPaid && (MainTest.isOriginal || MainTest.isFaultFound) ;
    }


    /**
     * Sets whether the deposit and remaining rent will be transferred between 
     * the old and new roommates.
     * 
     * @param   transferPaid    true indicates that the new tenant will pay 
     *                          the old tenant the deposit and remaining rent, 
     *                          false indicates that the landlord will give the 
     *                          deposit and remaining rent to the old roommate 
     *                          and collect it from the new roommate.
     */
    public void setTransferPaid(boolean transferPaid) {
        this.transferPaid = transferPaid;
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
    
        super.validate(propertyName, manager);
        if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
        else if (propertyName.equals("newTenant")) {
        	resident(propertyName, getNewTenant(), manager);
        }
    }
}
